package com.oocl.o2o.admin.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oocl.o2o.admin.dao.FailReasonDao;
import com.oocl.o2o.admin.dao.MerchantDao;
import com.oocl.o2o.admin.entity.FailReason;
import com.oocl.o2o.admin.entity.Merchant;
import com.oocl.o2o.admin.service.MerchantService;
import com.oocl.o2o.admin.util.Base64Unit;
import com.oocl.o2o.admin.util.FileUtil;
import com.oocl.o2o.admin.util.ImageUtil;
import com.oocl.o2o.admin.util.NotifyUtil;
import com.oocl.o2o.admin.vo.MerchantVO;
import com.oocl.o2o.connectutil.assist.ConfigUtil;
import com.oocl.o2o.connectutil.assist.HttpConnectionUtil;
import com.oocl.o2o.connectutil.dto.MerchantDTO;
import com.sun.mail.handlers.image_gif;

@Service(value = "merchantServiceImpl")
public class MerchantServiceImpl implements MerchantService {

	private static Logger logger = Logger.getLogger(MerchantServiceImpl.class);

	@Resource(name = "merchantDaoImpl")
	private MerchantDao merchantDao;

	@Resource(name = "failReasonDaoImpl")
	private FailReasonDao failReasonDao;

	@Override
	public Merchant findByName(String uname) {

		return merchantDao.findByName(uname);
	}

	@Override
	public List<Merchant> findByStatus(int status) {
		return merchantDao.findByStatus(status);
	}

	@Override
	public List<Merchant> findAll() {
		return merchantDao.findAll();
	}

	@Override
	@Transactional
	public Merchant add(Merchant merchant) {
		return merchantDao.add(merchant);
	}

	@Override
	@Transactional
	public Merchant update(Merchant merchant) {
		return merchantDao.update(merchant);
	}

	@Override
	public Merchant findById(String id) {
		return merchantDao.findById(id);
	}

	/**
	 * id-->身份证的type po-->头像的type
	 */
	@Override
	@Transactional
	public MerchantVO findByIdForDetail(String id) {
		// TODO Auto-generated method stub
		Merchant merchant = merchantDao.findById(id);
		String portraitSrc = ImageUtil.getImagePath(merchant.getUname(), "po");
		String idSrc = ImageUtil.getImagePath(merchant.getUname(), "id");
		File file = new File(portraitSrc);
		long filelastModifiedTime = file.lastModified();
		int advertimentSize = merchant.getAdvertisementSet().size();
		logger.info(advertimentSize);

		int complaintSize = merchant.getComplaintInfoSet().size();
		logger.info(complaintSize);
		logger.info(filelastModifiedTime);
		String failReason = null;
		if (merchant.getFailReason() != null) {
			failReason = merchant.getFailReason().getReasonContent();
		}
		MerchantVO merchantVO = new MerchantVO(merchant.getId(),
				merchant.getUname(), merchant.getAddress(),
				merchant.getStatus(), failReason, filelastModifiedTime,
				advertimentSize, complaintSize);
		merchantVO.setIdCard(idSrc);
		merchantVO.setPortrait(portraitSrc);
		return merchantVO;
	}

	@Override
	public String findByIdForShowImage(String id, String type) {
		// TODO Auto-generated method stub
		Merchant merchant = merchantDao.findById(id);
		String filePath = ImageUtil.getImagePath(merchant.getUname(), type);
		return filePath;
	}

	/**
	 * 驳回
	 * 
	 * @return
	 */
	@Transactional
	public Merchant changeStatusToThree(String id, String status, String content) {
		Merchant merchant = merchantDao.findById(id);
		merchant.setStatus(Integer.parseInt(status));
		FailReason failReason = new FailReason(merchant, content);
		failReasonDao.add(failReason);
		Merchant m = merchantDao.findById(id);
		return m;
	}

	@Override
	@Transactional
	public Merchant changeStatus(MerchantVO merchantVO) {
		// TODO Auto-generated method stub
		Merchant merchant = merchantDao.findById(merchantVO.getId());
		merchant.setStatus(merchantVO.getStatus());
		System.out.println(merchantVO.getReasonContent());
		if (merchantVO.getReasonContent() == null
				|| "".equals(merchantVO.getReasonContent())) {
			merchantDao.update(merchant);
			NotifyUtil.notifyAuditInfo(merchant.getUname(),merchantVO.getStatus(),"");
		} else {
			FailReason failReason = new FailReason(merchant,
					merchantVO.getReasonContent());
			failReasonDao.add(failReason);
			NotifyUtil.notifyAuditInfo(merchant.getUname(),merchantVO.getStatus(),merchantVO.getReasonContent());
		}
		Merchant m = merchantDao.findById(merchantVO.getId());
		
		return m;
	}

	@Override
	@Transactional
	public Merchant changeSta(String id, String status) {
		// TODO Auto-generated method stub
		Merchant merchant = merchantDao.findById(id);
		merchant.setStatus(Integer.parseInt(status));
		Merchant m = merchantDao.update(merchant);
		return m;
	}

	@Override
	@Transactional
	public Merchant register(MerchantDTO merchantDTO) {

		String uname = merchantDTO.getUname();
		Merchant merchant = merchantDao.findByName(uname);

		if (merchant == null) {
			// 解析封装
			byte[] idCard = Base64Unit.decode(merchantDTO.getIdCard());
			byte[] portrait = Base64Unit.decode(merchantDTO.getPortrait());
			merchant = new Merchant();
			merchant.setUname(merchantDTO.getUname());
			merchant.setAddress(merchantDTO.getAddress());
			merchant.setStatus(merchantDTO.getStatus());
			merchant.setIdCard(idCard);
			merchant.setPortrait(portrait);

			merchant = merchantDao.add(merchant);
			
			try {

				FileUtil.writeFile(idCard,
						ImageUtil.getImagePath(merchant.getUname(), "id"));
				FileUtil.writeFile(portrait,
						ImageUtil.getImagePath(merchant.getUname(), "po"));
			} catch (IOException e) {
				e.printStackTrace();
			}

		} else {

			byte[] idCard = Base64Unit.decode(merchantDTO.getIdCard());
			byte[] portrait = Base64Unit.decode(merchantDTO.getPortrait());
			merchant.setUname(merchantDTO.getUname());
			merchant.setAddress(merchantDTO.getAddress());
			merchant.setStatus(merchantDTO.getStatus());
			merchant.setIdCard(idCard);
			merchant.setPortrait(portrait);

			failReasonDao.deleteByMerchantId(merchant.getId());

			try {
				FileUtil.writeFile(idCard,
						ImageUtil.getImagePath(merchant.getUname(), "id"));
				FileUtil.writeFile(portrait,
						ImageUtil.getImagePath(merchant.getUname(), "po"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return merchant;
	}

}
